<template>
  <div>
    <el-dialog
      :title="info.isadd == true ? '秒杀添加' : '秒杀编辑'"
      :visible.sync="info.isshow"
      @closed="close"
    >
      <el-form :model="form">
        <el-form-item label="活动名称" label-width="120px">
          <el-input v-model="form.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="活动期限" label-width="120px">
          <div class="block">
            <!-- <span class="demonstration">默认</span> -->
            <el-date-picker
              v-model="value"
              type="datetimerange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            >
            </el-date-picker>
          </div>
        </el-form-item>
        <el-form-item label="一级分类" label-width="120px">
          <el-select
            v-model="form.first_cateid"
            placeholder="请选择"
            @change="change_first_cateid"
          >
            <el-option
              v-for="item in cateList"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="二级分类" label-width="120px">
          <el-select
            v-model="form.second_cateid"
            placeholder="请选择"
            @change="change_second_cateid"
          >
            <el-option
              v-for="item in second_cateid_list"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品" label-width="120px">
          <el-select v-model="form.goodsid" placeholder="请选择">
            <el-option
              v-for="item in goodsid_list"
              :key="item.id"
              :label="item.goodsname"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" label-width="120px">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="info.isshow = false">取 消</el-button>
        <el-button type="primary" @click="add" v-if="info.isadd"
          >提交</el-button
        >
        <el-button type="primary" v-else @click="edit">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  reqbanneradd,
  reqbanneredit,
  reqbannerinfo,
  reqcatelist,
  reqgoodslist,
  reqseckadd,
  reqseckedit,
  reqseckinfo,
} from "../../../request/api";
import { successAlert, errorAlert } from "../../../utils/alert";
import { mapActions, mapGetters } from "vuex";
import path from "path";
export default {
  props: ["info"],
  data() {
    return {
      value: [],
      form: {
        title: "",
        begintime: "",
        endtime: "",
        first_cateid: "",
        second_cateid: "",
        goodsid: "",
        status: 1,
      },
      second_cateid_list: [],
      goodsid_list: [],
    };
  },
  methods: {
    ...mapActions({
      reqList: "seck/reqList",
      reqCateList: "cate/reqList",
    }),
    empty() {
      this.form = {
        title: "",
        begintime: "",
        endtime: "",
        first_cateid: "",
        second_cateid: "",
        goodsid: "",
        status: 1,
      };
      this.value = [];
      this.second_cateid_list = [];
      this.goodsid_list = [];
    },
    change_first_cateid() {
      this.form.second_cateid = "";
      this.get_second_cate_list();
    },
    get_second_cate_list() {
      reqcatelist({ pid: this.form.first_cateid }).then((res) => {
        let list = res.data.list ? res.data.list : [];
        this.second_cateid_list = list;
      });
    },
    change_second_cateid() {
      this.form.goodsid = "";
      this.get_goodsid_list();
    },
    get_goodsid_list() {
      reqgoodslist({
        fid: this.form.first_cateid,
        sid: this.form.second_cateid,
      }).then((res) => {
        let list = res.data.list ? res.data.list : [];
        this.goodsid_list = list;
      });
    },

    add() {
      reqseckadd({
        ...this.form,
        begintime: new Date(this.value[0]).getTime(),
        endtime: new Date(this.value[1]).getTime(),
      }).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.empty();
          this.info.isshow = false;
          this.reqList();
        } else {
          errorAlert(res.data.msg);
        }
      });
    },

    getOne(id) {
      reqseckinfo({ id }).then((res) => {
        if (res.data.code == 200) {
          this.form = res.data.list;
          this.get_second_cate_list();
          this.get_goodsid_list();
          let arr1 = new Date(parseInt(this.form.begintime)).toJSON();
          let arr2 = new Date(parseInt(this.form.endtime)).toJSON();
          this.value = [arr1, arr2];
          this.form.id = id;
        }
      });
    },
    edit() {
      reqseckedit({
        ...this.form,
        begintime: new Date(this.value[0]).getTime(),
        endtime: new Date(this.value[1]).getTime(),
      }).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.info.isshow = false;
          this.empty();
          this.reqList();
        }
      });
    },
    close() {
      !this.info.isadd && this.empty();
    },
  },
  computed: {
    ...mapGetters({
      list: "banner/list",
      cateList: "cate/list",
    }),
  },
  mounted() {
    if (this.cateList.length == 0) {
      this.reqCateList();
    }
    console.log(new Date(1622131200000).toJSON(), "2222");
    // console.log(this.cateList);
  },
};
</script>

<style scoped lang="less">
/deep/ .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
